﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using Microsoft.VisualBasic;
using System.Web.Security;
using Code;
#pragma warning disable 436

public partial class Common_Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       /* if (!IsPostBack)
        {
            if (Request.Browser.Cookies == true)
            {
                if (Request.Cookies["UserInfo"] != null)
                {
                    String username = Request.Cookies["UserInfo"].Values[0];
                    WebMsgBox.Show(username + " đã đăng nhập rồi, không cần đăng nhập lại");
                }
            }
        }*/
    }

    public string HashPassword(string pwd)
    {
        string hashedPwd = FormsAuthentication.HashPasswordForStoringInConfigFile(pwd, "sha1");

        return hashedPwd;
    }

    protected void btnLogOn_Click(object sender, ImageClickEventArgs e)
    {
        MemberController memberController = new MemberController();
        int memID = memberController.CheckMemberAccount(txtUsername.Text, HashPassword(txtPassword.Text));
        if (memID != -1)
        {
            Session.Add("Username", txtUsername.Text);
            Session["UserID"] = memID;
            Session["IsLogin"] = 1;

            if (cbRememberAccount.Checked == true)
            {
                HttpCookie cookie = new HttpCookie("UserInfo");
                if (Request.Browser.Cookies == true)
                {
                    if (Request.Cookies["UserInfo"] != null)
                    {
                        cookie.Expires = DateTime.Now.AddDays(-1);
                    }
                    else
                    {
                        cookie["UserInfo"] = txtUsername.Text;
                        cookie.Expires = DateTime.Now.AddDays(30);
                        Response.Cookies.Add(cookie);
                    }
                }
            }
            else
            {
                HttpCookie aCookie = new HttpCookie("UserInfo");
                aCookie.Expires = DateTime.Now.AddDays(-1);
                Response.Cookies.Add(aCookie);
            }

            string url;
            String strLoaiNguoiDung = memberController.GetMemberType(txtUsername.Text);
            switch (strLoaiNguoiDung)
            {
                case "Admin":
                    Session["Authentication"] = 0;
                    break;
                case "Employee":
                    Session["Authentication"] = 1;
                    break;
                case "Member":
                    Session["Authentication"] = 2;
                    break;
            }
            url = "SuccessfulLogin.aspx?username=" + txtUsername.Text;
            Response.Redirect(url);
        }
        else
        {
            WebMsgBox.Show("Tên đăng nhập hoặc mật khẩu không chính xác, vui lòng đăng nhập lại!");
        }
    }
}